import { RedisClient, createClient } from "redis";

class RedisPublisher {
  private host: string;
  private port: number;
  private client: RedisClient;

  constructor(host: string, port: number) {
    this.host = host;
    this.port = port;

    this.client = createClient(port, host);
  }

  publish(channel: string, message: any): void {;
    this.client.publish(channel, JSON.stringify(message));
  }

  async dispose() {
    const fn = (): void => {
      this.client.unsubscribe();
      this.client.quit();
    };

    await fn();
  }
}

export { RedisPublisher };
